package com.service;

import com.dao.CartDao;
import com.entity.Carts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author liuronghua
 * @version 1.0
 * @data 2020-09-2020/9/22 10:17
 * @desc
 */
@Service
public class CartService {
    @Autowired
    private CartDao cartDao;

    @Autowired
    private GoodService goodService;

    /**
     * 获取购物车列表
     * @param userId
     * @return
     */
    public List<Carts> getList(int userId){
        return pack(cartDao.selectListByUserId(userId));
    }

    public int getCount(int userId){
        return cartDao.selectSumAmountByUserId(userId);
    }

    private List<Carts> pack(List<Carts> list) {
        for (Carts cart:list){
            cart=pack(cart);
        }
        return list;
    }

    private Carts pack(Carts cart){
        if (Objects.nonNull(cart)){
            cart.setGood(goodService.getid(cart.getGoodId()));
            cart.setTotal(cart.getAmount() * cart.getGood().getPrice());
        }
        return cart;
    }

    public int getTotal(int userId) {
        int total=0;
        List<Carts> cartList=this.getList(userId);
        if (Objects.nonNull(cartList)&& !cartList.isEmpty()){
            for (Carts cart : cartList){
                total +=cart.getGood().getPrice() * cart.getAmount();
            }
        }
        return total;
    }

    /**
     * 数量+1
     * @param id
     * @return
     */
    public boolean add(int id){
        return cartDao.updateAmonut(id,1);
    }

    /**
     * 数量-1
     * @param id
     * @return
     */
    public boolean less(int id){
        Carts cart=cartDao.select(id);
        if (cart.getAmount()<=1){
            return delete(id);
        }
        return cartDao.updateAmonut(id,-1);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean delete(int id){
        return cartDao.delete(id);
    }

    /**
     * 清空购物车
     * @param userId
     * @return
     */
    public boolean clean(Integer userId){
        return cartDao.deleteByUserId(userId);
    }

    /**
     * 添加到购物车
     * @param goodId
     * @param userId
     * @return
     */
    public boolean save(int goodId,int userId){
        Carts cart=cartDao.selectByUserIdAndGoodId(userId,goodId);
        if (Objects.nonNull(cart)){
            return cartDao.updateAmonut(cart.getId(),1);
        }
        cart=new Carts();
        cart.setGoodId(goodId);
        cart.setUserId(userId);
        cart.setAmount(1);
        return cartDao.insert(cart);
    }
}
